/*
 * $Id: FooServiceIT.java 77 2011-02-07 06:11:38Z samaxes $
 *
 * Copyright 2010 samaxes.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.samaxes.stripes.test;

import static org.jboss.arquillian.api.RunModeType.AS_CLIENT;
import static org.testng.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.logging.Logger;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.api.Run;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.annotations.Test;

import com.samaxes.stripes.action.FooActionBean;
import com.samaxes.stripes.business.FooService;
import com.samaxes.stripes.business.FooServiceBean;
import com.samaxes.stripes.ejb3.EJBBean;
import com.samaxes.stripes.ejb3.EJBHelper;
import com.samaxes.stripes.ejb3.EJBInjectionPostProcessor;
import com.samaxes.stripes.ext.CustomActionBeanContext;

/**
 * Example service test class.
 * 
 * @author Samuel Santos
 * @version $Revision$
 */
@Run(AS_CLIENT)
public class FooServiceIT extends Arquillian {

    private static final Logger LOGGER = Logger.getLogger(FooServiceIT.class.getName());

    @Deployment
    public static EnterpriseArchive createDeployment() throws IOException {
        final JavaArchive ejbJar = ShrinkWrap.create(JavaArchive.class, "ejb.jar").addClasses(FooService.class,
                FooServiceBean.class);
        final WebArchive war = ShrinkWrap.create(WebArchive.class, "foo.war").addClasses(EJBBean.class,
                EJBInjectionPostProcessor.class, EJBHelper.class, FooActionBean.class, CustomActionBeanContext.class)
                .addLibraries(MavenArtifactResolver.resolve("net.sourceforge.stripes:stripes:1.6.0-SNAPSHOT"))
                .addResource("index.jsp").setWebXML("web.xml");
        final EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "stripes-ejb3.ear").addModule(ejbJar)
                .addModule(war);

        LOGGER.info(ear.toString(true));
        exportArchive(ear);

        return ear;
    }

    @Test
    public void shouldGreetUser() throws IOException {
        final String name = "Earthlings";
        final URL url = new URL("http://localhost:8080/foo/Foo.action");
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String line;

        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        reader.close();

        LOGGER.info("Returned response: " + builder.toString());
        assertEquals(builder.toString(), FooService.GREETING + name);
    }

    private static void exportArchive(Archive<?> archive) throws IOException {
        InputStream zipStream = archive.as(ZipExporter.class).exportZip();
        OutputStream out = new FileOutputStream("target/" + archive.getName() + ".zip");
        byte[] buffer = new byte[4096];
        int length;

        while ((length = zipStream.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
    }
}
